package cn.lx.controller;

import cn.lx.common.constants.Constants;
import cn.lx.common.response.Response;
import cn.lx.ILoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName LoginController
 * @Description: 登录controller
 * @Author: 落雪
 * @CreateDate: 2025/3/14 09:17
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/3/14 09:17
 * @UpdateRemark:
 * @Version: 1.0
 */
@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/v1/login/")
public class LoginController {

    @Resource
    private ILoginService loginService;

    /**x
    * @Author: 落雪
    * @Date: 2025/3/14 09:32
    * @Description: 生成二维码
    * @param
    * @Return: cn.lx.common.response.Response<java.lang.String>
    * @Version: 1.0.0
    */
    @RequestMapping(value = "weixinQrCodeTicket", method = RequestMethod.GET)
    public Response<String> weixinQrCodeTicket(){

        try {
            String qrCodeTicket = loginService.createQrCodeTicket();
            log.info("生成微信扫码登录 ticket:{}", qrCodeTicket);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.SUCCESS.getCode())
                    .info(Constants.ResponseCode.SUCCESS.getInfo())
                    .data(qrCodeTicket)
                    .build();
        } catch (Exception e) {
            log.error("生成微信扫码登录 ticket 失败", e);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }


    /**
    * @Author: 落雪
    * @Date: 2025/3/14 09:34
    * @Description: 检查登录状态
    * @param ticket
    * @Return: cn.lx.common.response.Response<java.lang.String>
    * @Version: 1.0.0
    */
    @RequestMapping(value = "checkLogin", method = RequestMethod.GET)
    public Response<String> checkLogin(@RequestParam String ticket){

        try {
            String openidToken = loginService.checkLogin(ticket);
            log.info("扫码检测登录结果 ticket:{} openidToken:{}", ticket, openidToken);
            if (StringUtils.isNotBlank(openidToken)) {
                return Response.<String>builder()
                        .code(Constants.ResponseCode.SUCCESS.getCode())
                        .info(Constants.ResponseCode.SUCCESS.getInfo())
                        .data(openidToken)
                        .build();
            } else {
                return Response.<String>builder()
                        .code(Constants.ResponseCode.NO_LOGIN.getCode())
                        .info(Constants.ResponseCode.NO_LOGIN.getInfo())
                        .build();
            }
        } catch (Exception e) {
            log.error("扫码检测登录结果失败 ticket:{}", ticket, e);
            return Response.<String>builder()
                    .code(Constants.ResponseCode.UN_ERROR.getCode())
                    .info(Constants.ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }
}
